import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class UserListPage extends Utils {

    private static String url = "http://47.108.48.236:8080/user-list.html";

    public UserListPage() {
        super(url);
    }

    /**
     * 未登录状态下访问人员列表页
     */
    public void noLoginToUserListPage() {
        // 处理弹窗
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        // 判断是否返回到登录页面
        String expect = webDriver.getTitle();
        assert expect.equals("管理员登录页面");
    }

    /**
     * 检查人员列表页是否加载成功
     */
    public void userListPageRight() {
        // 1. 进行登录
        webDriver.findElement(By.cssSelector("#phoneNumber")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#phoneNumber")).sendKeys("13311111111");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("13311111111");
        webDriver.findElement(By.cssSelector("#loginForm > button")).click();
        // 2. 等待页面加载
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userList")));
        // 3. 导航到人员列表页
        webDriver.findElement(By.cssSelector("#userList")).click();
        // 4. 查看列表元素
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#userList")));
        getScreenShot(getClass().getName());
    }
}
